package com.kingtone.rongji.adapter;

import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kingtone.rongji.ProductDetailActivity;
import com.kingtone.rongji.R;
import com.kingtone.rongji.biz.Product;
import com.kingtone.rongji.tool.AppRestClient;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;


public class OrdeDetailProudctAdapter extends BaseAdapter
{
	private Context context = null;
	private ArrayList<Product> prodctList = null;
	private LayoutInflater inflater; 
    private Product product;
	public OrdeDetailProudctAdapter(Context context, ArrayList<Product> prodctList)
	{
		this.inflater=LayoutInflater.from(context);
		this.context = context;
		this.prodctList = prodctList;
	}
	@Override
	public int getCount()
	{
		return prodctList.size();
	}

	@Override
	public Object getItem(int position)
	{
		return prodctList.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View view;
		 ViewHolder holder = null;
		//如果已加载过，使用缓存技术不再重新加载
		if(convertView!=null){
			view=convertView;
			holder=(ViewHolder) view.getTag();
		}else{
			view=inflater.inflate(R.layout.order_detail_product_item, null);
			holder = new ViewHolder();
		}
		holder.lineLayout = (LinearLayout)view.findViewById(R.id.order_detail_product_layout);
		holder.productId = (TextView)view.findViewById(R.id.order_detail_product_id);
		holder.productImage =(ImageView) view.findViewById(R.id.order_detail_product_icon);
		holder.productName = (TextView) view.findViewById(R.id.order_detail_product_name);
		holder.prodctNum =(TextView) view.findViewById(R.id.order_detail_product_num);
		holder.prodctTotalPrice =(TextView) view.findViewById(R.id.order_detail_product_total_price);
	
		
		view.setTag(holder);
		product = prodctList.get(position); 
        
		holder.productId.setText(context.getString(R.string.order_detail_product_id) + product.getProductId());
		ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context));
		String productIcon = AppRestClient.convertImageUrl(product.getProductId(), product.getImageName());
		if(!TextUtils.isEmpty(productIcon))
		{
			ImageLoader.getInstance().displayImage(productIcon, holder.productImage);
		}
		final String productId = product.getProductId();
		holder.lineLayout.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(context, ProductDetailActivity.class);
				intent.putExtra("productId", productId);
				context.startActivity(intent);
			}
		});
		holder.productName.setText(product.getProductName());
		holder.prodctNum.setText(context.getString(R.string.order_detail_product_num)+ product.getQuantity());
		holder.prodctTotalPrice.setText("" + product.getQuantity() + " × " + product.getUnitPrice() + " = ¥ " + product.getQuantity()*product.getUnitPrice());
		
		
		return view;
	}
	
	private class ViewHolder
	{
		public LinearLayout lineLayout;
		public ImageView productImage;
		public TextView productId;
		public TextView productName;
		public TextView prodctNum;
		public TextView prodctTotalPrice;
	}
}
